package android.slc.appbase.ui.vd;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.activity.result.contract.ActivityResultContract;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.io.Serializable;

/**
 * 简单常用的注册activity方式，用于快速注册activity、传递参数、接收activity返回数据
 *
 * @Description:
 * @Author: sunlunchang
 * @CreateDate: 2022/10/12 下午 1:55
 */
public class SimpleRegisterActivityFunc<T extends Serializable> extends BaseRegisterActivityFunc<T> {

    protected ActivityResultContract<Bundle, Bundle> targetActivityResultContract = new ActivityResultContract<Bundle, Bundle>() {
        @NonNull
        @Override
        public Intent createIntent(@NonNull Context context, Bundle input) {
            if (getIntentTargetWrapper() == null) {
                throw new NullPointerException("intentTargetWrapper不能为空");
            }
            Intent intent;
            if (getIntentTargetWrapper().isTargetClassMode()) {
                intent = new Intent(context, getIntentTargetWrapper().getTargetClass());
            } else if (getIntentTargetWrapper().isIntentActionMode()) {
                intent = new Intent(getIntentTargetWrapper().getIntentAction());
            } else {
                throw new NullPointerException("intentTargetWrapper不能为空");
            }
            if (input != null) {
                intent.putExtras(input);
            }
            return intent;
        }

        @Override
        public Bundle parseResult(int resultCode, @Nullable Intent intent) {
            if (resultCode == Activity.RESULT_OK && intent != null) {
                return intent.getExtras();
            }
            return null;
        }
    };

    public SimpleRegisterActivityFunc(IntentTargetWrapper intentTargetWrapper) {
        this(intentTargetWrapper, null);
    }

    public SimpleRegisterActivityFunc(IntentTargetWrapper intentTargetWrapper, OnActivityResultCallback<T> OnActivityResultCallback) {
        super(intentTargetWrapper, OnActivityResultCallback);
    }

    @Override
    public ActivityResultContract<Bundle, Bundle> getTargetActivityResultContract() {
        return targetActivityResultContract;
    }

}
